package com.andy.eqm.server.game;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.StreamUtils;

import com.andy.eqm.server.dao.DaoUtils;
import com.andy.eqm.server.entity.ConfCommon;
import com.andy.eqm.server.entity.ConfItem;
import com.andy.eqm.server.entity.ConfMake;
import com.andy.eqm.server.entity.ConfProbability;
import com.andy.eqm.server.utils.JsonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class GameConf {

	
	private static Map<String,ConfCommon> confCommons;
	private static Map<Integer,ConfItem> confItems;
	private static Map<Integer,ConfMake> confMakes;
	private static Map<Integer,List<ConfProbability>> confProbabilitys;
	
	public static void init() {
		
		// 初始化概率
		initProbabilitys();
		// 初始化物品配置
		initItems();
		
		// 初始化合成配置
		initMakes();
		
		// 初始化基础配置
		initCommons();
		
		
	}
	
	private static void initMakes() {
		Map<Integer,ConfMake> confMakesT = Maps.newHashMap();
		List<ConfMake> items = DaoUtils.getList(ConfMake.class);
		for(ConfMake item:items){
			confMakesT.put(item.getItemId(), item);
		}
		confMakes = confMakesT;
	}

	private static void initItems() {
		Map<Integer,ConfItem> confItemsT = Maps.newHashMap();
		List<ConfItem> items = DaoUtils.getList(ConfItem.class);
		for(ConfItem item:items){
			confItemsT.put(item.getItemId(), item);
		}
		confItems = confItemsT;
	}
	
	public static ConfItem getConfItem(Integer itemId){
		return confItems.get(itemId);
	}

	private static void initProbabilitys() {
		Map<Integer,List<ConfProbability>> confProbabilitysT = Maps.newHashMap();
		List<ConfProbability> probabilities = DaoUtils.getList(ConfProbability.class);
		for(ConfProbability probability:probabilities){
			List<ConfProbability> confs = confProbabilitysT.get(probability.getGroupId());
			if(confs == null){
				confs = Lists.newArrayList();
				confProbabilitysT.put(probability.getGroupId(), confs);
			}
			confs.add(probability);
		}
		confProbabilitys = confProbabilitysT;
	}

	public static List<ConfProbability> getPropGroup(Integer multiple) {
		return confProbabilitys.get(multiple);
	}



	public static void initCommons() {
		Map<String,ConfCommon> confCommonsT = Maps.newHashMap();
		List<ConfCommon> commons = DaoUtils.getList(ConfCommon.class);
		for(ConfCommon comm:commons){
			confCommonsT.put(comm.getNid(), comm);
		}
		confCommons = confCommonsT;
		initConf();
	}

	public static String confJsonUrl;
	public static void initConfJson(){
		Map<String,Object> confs = Maps.newHashMap();
		String confJson = JsonUtils.object2JsonString(confs);
		try {
			File confFile = File.createTempFile("confJson_", ".json");
			StreamUtils.copy(confJson, Charset.defaultCharset(), new FileOutputStream(confFile));
			confJsonUrl = confFile.getAbsolutePath();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static int version; // 配置版本
	
	public static void initConf() {
		Global.sysCurrHave = new AtomicLong(getConfInt("sys_curr_have"));
		
		int versionT = getConfInt("conf_version");
		if(versionT > version){
			version = versionT;
			// 生成配置json
			initConfJson();
		}
	}
	
	public static int getConfInt(String nid){
		String v = getConfStr(nid);
		if(StringUtils.isNotBlank(v)){
			return Integer.parseInt(v);
		}
		return 0;
	}
	public static String getConfStr(String nid){
		ConfCommon common = confCommons.get(nid);
		if(common != null){
			return common.getValue();
		}
		return "";
	}

	public static ConfMake getConfMake(int itemId) {
		return confMakes.get(itemId);
	}


	
	
}
